/*
 * This work is licensed under a GNU General Public License 
 * (http://www.gnu.org/copyleft/gpl.html).
 * 
 * Title: 		Mobup
 * Version: 		0.3
 * Description:         Mobile uploader for Flickr
 * Creator:             Vincenzo Lembo and Matteo Penzo
 * Source:              http://mobup.org
 *
 */


package uns.fit.gis.ui;


import java.io.DataInput;
import javax.microedition.lcdui.*;

import uns.fit.gis.MapDrawer;
import uns.fit.gis.i18n.LocaleManager;
import uns.fit.gis.mapdrawer.MapCanvas;
import uns.fit.gis.upload.PhotoUploader;
import uns.fit.gis.upload.PhotoUploaderListener;

import ctmobup.flickr.*;
import ctmobup.utils.*;

import java.util.*;


/*
 * PhotoForm Class
 *
 * Form for set the Title, Description and Tags for captured image
 *
 */

public class PhotoForm extends Form implements CommandListener, PhotoUploaderListener {
    
    private MapDrawer midlet;
    private byte[] dataImage;
    private Flickr flickr;
    private Command aboutCommand;
    private Command tagsCommand;
    private Command setsCommand;
    private Command groupsCommand;
    private Command backCommand;
    private Command uploadToFlickr;
    private Command uploadToOurServer;
    
    private TextField itemTitle;
    private TextField itemDesc;
    private TextField itemTags;
    public String Title;
    public String Desc;
    public String Tags;
    
    private String defTitle = "Another wonderful Mobile Map 05 picture";
    private String defDesc = "Uploaded with Mobile Map 05";
    
    private PhotoUploader uploader;
    
    public PhotoForm(byte[] dataImg){
        super(LocaleManager.getMessage("title"));
        this.dataImage = dataImg;
        this.midlet = MapDrawer.getMidlet();
        itemTitle = new TextField(LocaleManager.getMessage("name"), defTitle, 1000, TextField.ANY);
        itemDesc = new TextField(LocaleManager.getMessage("comment"), defDesc, 1000, TextField.ANY);
        itemTags = new TextField(LocaleManager.getMessage("tags"), "", 1000, TextField.ANY);
        append(itemTitle);
        append(itemDesc);
	    append(itemTags);
        //creating flickr object to upload the image
        flickr = new Flickr(MapDrawer.FlikcrApiKey, MapDrawer.FlickrSecretShared);
        setCommandListener(this);
        /*
        aboutCommand=new Command("About",Command.SCREEN,1);
        addCommand(aboutCommand);
        setsCommand=new Command("Select photosets",Command.SCREEN,2);
        addCommand(setsCommand);
        tagsCommand=new Command("Select tags",Command.SCREEN,3);
        addCommand(tagsCommand);
        groupsCommand=new Command("Select groups",Command.SCREEN,4);
        addCommand(groupsCommand);
       	*/
        backCommand=new Command(LocaleManager.getMessage("back"),Command.BACK,4);
        addCommand(backCommand);
        uploadToFlickr = new Command("Upload to Flickr", Command.ITEM, 1);
        uploadToOurServer = new Command("Upload to Our Server", Command.ITEM, 1);
        addCommand(uploadToFlickr);
        addCommand(uploadToOurServer);
    }
    
    
    public void commandAction(Command c, Displayable displayable) {
        SetField();
        if(c == uploadToFlickr) 
        {
            Clear();
            //display message for wait 
            SetMessage("\n\n\n\nPlease wait for the photo to be uploaded on Flickr...\n");
            new Thread() {
            	public void run() {
            		SendPhoto();
            	}
            }.start();
            
        }
        else if (c == uploadToOurServer) 
        {
            //TODO: Upload to our server
        	if(MapCanvas.earthPosition != null) {
        		uploader = new PhotoUploader();
        		uploader.setListener(this);
        		uploader.startUpload(1, this.Title, this.Desc, MapCanvas.earthPosition.getLatitude().toString(), MapCanvas.earthPosition.getLongitude().toString(), dataImage);
        	}
        	else {
        		MapDrawer.showAlert("Non GPS data");
        	}
        }
        else if (c == aboutCommand) 
        {
           
        }
        else if (c == tagsCommand) 
        {
            
        }
        else if (c == setsCommand) 
        {
            
        }
        else if (c == groupsCommand) 
        {
            
        }
        else if (c == backCommand) 
        {
            midlet.showVideoCanvas();
        }
    }
    
    /*
     * Send the image
     */
    public void SendPhoto()
    {
        String photoid = "";
        try
        {
//#ifdef JSR82
           if(MapCanvas.earthPosition != null){
       		Tags+=" geotagged geo:lon="+ MapCanvas.earthPosition.getLongitude().toString()+" geo:lat="+ MapCanvas.earthPosition.getLatitude().toString();
       	}
//#endif        
            //send the image
            photoid = flickr.SendPhoto(this.dataImage,this.Title,this.Desc,this.Tags);
        }
        catch(Exception e)
        {
            MessageBox.Show(midlet,LocaleManager.getMessage("upload"),LocaleManager.getMessage("uploadfailed"),AlertType.ERROR,2000);
            DisplayCameraCanvas();
        }
        //check for error in upload
        if(photoid == "") {
            MessageBox.Show(midlet,LocaleManager.getMessage("upload"),LocaleManager.getMessage("uploadfailed"),AlertType.ERROR,2000);
        }
        else {
        	MapDrawer.showAlert(LocaleManager.getMessage("uploadsucceeded"));
        }
        DisplayCameraCanvas();
    }
    
    /*
     * Clear all item on the form
     */
    public void Clear()
    {
        
    }
  
    /*
     * Rebuild the form with the text field
     */
    public void Restore()
    {
        Clear();
        
    }
    
    /*
     * Get the value of the textfields
     */
    public void SetField()
    {
        this.Title = itemTitle.getString();
        this.Desc = itemDesc.getString();
        this.Tags = itemTags.getString();
    }
    
    /*
     * Set the tags field 
     */
    public void SetTags(String tags)
    {
        
    }
    
    /*
     * Set the message for user
     */
    private void SetMessage(String msg)
    {
        append(msg);
    }
    
    /*
     * Display the camera canvas 
     * The task is delayed for display messages
     */
    private void DisplayCameraCanvas()
    {
        midlet.showVideoCanvas();
    }


	public void uploadFailed() {
		// TODO Auto-generated method stub
		MapDrawer.showAlert(LocaleManager.getMessage("uploadfailed"));
	}


	public void uploadFinished() {
		// TODO Auto-generated method stub
		MapDrawer.showAlert(LocaleManager.getMessage("uploadsucceeded"));
	}
}
